package cn.com.infohold.smartcity.activity.Fabaofang;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.Map;

import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.api.HttpEvent;
import cn.com.infohold.smartcity.api.SDK;
import cn.com.infohold.smartcity.api.bean.FaBaoFangBean;
import cn.com.infohold.smartcity.api.bean.LandResult;
import cn.com.infohold.smartcity.data.XmlData;
import cn.com.infohold.smartcity.parent.ParentActivity;
import cn.com.infohold.smartcity.utils.GsonUtil;
import cn.com.infohold.smartcity.utils.RequestCode;
import common.annotation.ViewInject;
import common.dialog.ProgressDialog;

/**
 * Created by Monsoon on 2017/11/8.
 */
public class FaBaoFangAdd_Activity extends ParentActivity{

    @ViewInject(R.id.ib_back)
    ImageButton ib_back;
    @ViewInject(R.id.et_code)
    EditText et_code;
    @ViewInject(R.id.et_shortname)
    EditText et_shortname;
    @ViewInject(R.id.et_fullname)
    EditText et_fullname;
    @ViewInject(R.id.et_parentpartname)
    EditText et_parentpartname;
    @ViewInject(R.id.et_wheelman)
    EditText et_wheelman;
    @ViewInject(R.id.et_address)
    EditText et_address;
    @ViewInject(R.id.et_tel)
    EditText et_tel;
    @ViewInject(R.id.et_zipcode)
    EditText et_zipcode;
    @ViewInject(R.id.btn_commit)
    Button btn_commit;

    private FaBaoFangBean faBaoFangBean;

    @Override
    protected Object inflateView() {
        return R.layout.activity_add_fabaofang;
    }

    @Override
    protected void initView(View anchor) {

        faBaoFangBean = new FaBaoFangBean();

        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                faBaoFangBean.setCode(et_code.getText().toString());
                faBaoFangBean.setShortname(et_shortname.getText().toString());
                faBaoFangBean.setFullname(et_fullname.getText().toString());
                faBaoFangBean.setParentpartname(et_parentpartname.getText().toString());
                faBaoFangBean.setWheelman(et_wheelman.getText().toString());
                faBaoFangBean.setAddress(et_address.getText().toString());
                faBaoFangBean.setTel(et_tel.getText().toString());
                faBaoFangBean.setZipcode(et_zipcode.getText().toString());
                faBaoFangBean.setStatus("1");
                faBaoFangBean.setManager(XmlData.getInstance().getUserInfo().getId());

                initData(faBaoFangBean);
            }
        });

    }

    private void initData(FaBaoFangBean bean) {
        SDK.api().addFabaofang(bean).postEvent(RequestCode.REQUEST_CODE_ADD_FABAOFANG);
        ProgressDialog.show(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(HttpEvent<String> httpEvent) {

        int requestCode = httpEvent.getRequestCode();
        if (requestCode != RequestCode.REQUEST_CODE_ADD_FABAOFANG) {
            ProgressDialog.dismiss();
            return;
        }

        if (httpEvent.getState() != HttpEvent.STATE_SUCCESS) {
            ProgressDialog.dismiss();
            return;
        } else {
            switch (requestCode) {
                case RequestCode.REQUEST_CODE_ADD_FABAOFANG:
                    ProgressDialog.dismiss();
                    Type type = new TypeToken<LandResult<Boolean>>() {
                    }.getType();
                    LandResult<Boolean> result = GsonUtil.fromJson(httpEvent.getData(), type);
                    Toast.makeText(this, result.getMessage(), Toast.LENGTH_SHORT).show();
                    if (result.getStatus().equals("1")) {
                            finish();
                    }

                    break;
                default:
                    break;
            }
        }
    }

}
